#include "globals.h"
#include "state_cursor.h"
#include "state_placetile.h"
#include "game.h"
#include <stdio.h>

void State_Cursor::triggerNextTurn()
{
	m_game->nextTurn();
	m_game->queueState(new State_PlaceTile);
	setDone(true);
}

void State_Cursor::init(Game * g)
{
	GameState::init(g);
}

void State_Cursor::processInput(int key){
	triggerNextTurn();
	Vector2 old = cursor;
	switch(key)
	{
	case 'w':	cursor.y--;	break;
	case 'a':	cursor.x--;	break;
	case 's':	cursor.y++;	break;
	case 'd':	cursor.x++;	break;
	case ' ':	triggerNextTurn();	return;
	}
	Board * b = m_game->getBoard();
	Vector2 c = cursor;
	if(c.x < 0 || c.x >= b->getWidth() || c.y < 0 || c.y >= b->getHeight())
	{
		cursor = old;
	}
}

void State_Cursor::draw(Graphics * g)
{
	Tile * t = m_game->getBoard()->getTile(cursor.x, cursor.y);
	char * text = t->getCo()?t->getCo()->getName():Tile::DEFAULT_UNPLACED;
	t->draw(g, text, m_game->flashColor(1));
}